<?php
/**
 * 数据过滤
 * @copyright reginx.com
 * $Id: filter.class.php 1466 2012-09-26 14:03:08Z lan7 $
 */
class filter{
    
    /**
     * 数据验证规则
     *
     * @var unknown_type
     */
    public static $rules = array(
        //键名小写
        'require'   => '/^.+$/',
        'email'     => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
        'phone'     => '/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/',
        'mobile'    => '/^((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/',
        'url'       => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/',
        'currency'  => '/^\d+(\.\d+)?$/',
        'number'    => '/\d+$/',
        'zip'       => '/^[1-9]\d{5}$/',
        'qq'        => '/^[1-9]\d{4,12}$/',
        'integer'   => '/^[-\+]?\d+$/',
        'double'    => '/^[-\+]?\d+(\.\d+)?$/',
        'english'   => '/^[A-Za-z]+$/',
        'uid'       => '/^[A-Za-z0-9_]{2,30}$/',
        'passwd'    => '/^[^\s]{2,37}$/',
        'all'       => '/^.*$/',
        'price'     => '/^\d+(\.\d+)?$/'
    );
    
    /**
     * 获取安全文本
     * 
     * @param unknown_type $str
     */
    public static function text(&$str){
        $str = trim($str);
        if($str != ''){
            self::loseTags($str);
            $str = htmlspecialchars(preg_replace('/\&.+?\;/i' , '' , $str) , ENT_QUOTES , $GLOBALS['_APP']['charset']);
        }
    }

    /**
     * 过滤html标签
     *
     * @param unknown_type $str
     */
    public static function loseTags(&$str){
        $str = htmlspecialchars_decode($str);
        $str = strip_tags($str);
        $str = preg_replace('/\<style(.*?)>.*?\<\/style\>/i'  ,'',$str);
        $str = preg_replace('/\<script(.*?)>.*?\<\/script\>/i','',$str);
    }

    /**
     * 默认过滤
     *
     * @param unknown_type $str
     * @return unknown
     */
    public static function normal(&$str){
        self::loseTags($str);
        $str = htmlspecialchars($str , ENT_QUOTES , $GLOBALS['_APP']['charset']);
    }

    /**
     * 转换成整数
     *
     * @param unknown_type $str
     */
    public static function int(&$str){
        $str = intval($str);
    }
}
?>